﻿using ProjBackEnd.Areas.Financial.Models;
using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Financial.Controllers
{
    /// <summary>
    /// 审批操作
    /// </summary>
    public class ApprovalOptController : ApiController
    {
        /// <summary>
        /// 获取待审批和已审批的业务账单
        /// eg.: 0000000001|TST1|WEB|20|1|0||0|R|N|0000000009
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|账单编号（0=所有）|制单人id（空值=所有）|客户id（0=所有）|收付标志（R-收；P-付；空值=所有）|审批标志（Y-已审；N-待审）|审批人id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/approvalbill")]
        public RespBill GetSelfApprovalBill(string key)
        {
            RespBill resp = new RespBill();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[6];
            string strRplb = strsKeyOriginal[8].ToUpper();
            string strRevlb = strsKeyOriginal[9];
            string strReviewer = strsKeyOriginal[10];
            int iBillid, iCusid, iPageSize, iCurrPage;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iBillid) || !int.TryParse(strsKeyOriginal[7], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetApprovalBillExtInfo(strReviewer, strCreateid, strComcd, iPageSize, iCurrPage, iBillid, iCusid, strRplb, strRevlb);
            return resp;
        }

        /// <summary>
        /// 获取待审批和已审批的结算单
        /// eg.: 0000000001|TST1|WEB|20|1|0||0|N|0000000009
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|每页记录数|当前页数（从1开始）|结算单编号（0=所有）|制单人id（空值=所有）|客户id（0=所有）|审批标志（Y-已审；N-待审）|审批人id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/approvalfn")]
        public RespFeeNotice GetSelfApprovalFn(string key)
        {
            RespFeeNotice resp = new RespFeeNotice();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 10)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strCreateid = strsKeyOriginal[6];
            string strRevlb = strsKeyOriginal[8];
            string strReviewer = strsKeyOriginal[9];
            int iFnid, iCusid, iPageSize, iCurrPage;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iFnid) || !int.TryParse(strsKeyOriginal[7], out iCusid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetApprovalFnExtInfo(strReviewer, strCreateid, strComcd, iPageSize, iCurrPage, iFnid, iCusid, strRevlb);
            return resp;
        }

        /// <summary>
        /// 获取审批拒绝意见列表
        /// eg.：0000000001|TST1|WEB|1|BILL
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|申请单id|申请单类别（BILL-业务账单；FN-付费联系单）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/approvaldeny")]
        public RespReviewDeny GetReviewDeny(string key)
        {
            RespReviewDeny resp = new RespReviewDeny();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 5)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strApplytp = strsKeyOriginal[4];
            int iApplyid;
            if (!int.TryParse(strsKeyOriginal[3], out iApplyid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iApplyid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。申请单流水号是必需的";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetApprovalDenyInfo(iApplyid, strApplytp);
            return resp;
        }

        /// <summary>
        /// 获取待审批的申请单数量
        /// eg.：0000000001|TST1|WEB|BILL
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|申请单类别（BILL-业务账单；FN-付费联系单）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/financial/approvalcount")]
        public RespApprovalCount GetApprovalCount(string key)
        {
            RespApprovalCount resp = new RespApprovalCount();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strApplytp = strsKeyOriginal[3];

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = financialLogic.GetApprovalCount(strMemberid, strApplytp);
            return resp;
        }


        /// <summary>
        /// 提交账单审批结果
        /// </summary>
        /// <param name="_reqInfo">审批结果信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/approvalsubmitbill")]
        public RespFinaNullResultInfo BillApprovalSubmit(ReqApplyApproval _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = (RespFinaNullResultInfo)financialLogic.BillApprove(_reqInfo.memberid, _reqInfo.applyid, _reqInfo.revlb, _reqInfo.revsort, _reqInfo.reason);
            return resp;
        }

        /// <summary>
        /// 提交结算单审批结果
        /// </summary>
        /// <returns>审批结果信息</returns>
        [HttpPost]
        [Route("api/financial/approvalsubmitfn")]
        public RespFinaNullResultInfo FnApprovalSubmit(ReqApplyApproval _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = (RespFinaNullResultInfo)financialLogic.FnApprove(_reqInfo.memberid, _reqInfo.applyid, _reqInfo.revlb, _reqInfo.revsort, _reqInfo.reason);
            return resp;
        }

        /// <summary>
        /// 结算单状态退回，用于审批完结后的结算单特殊处理
        /// 敏感操作，一退到底
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/approvalbackfn")]
        public RespFinaNullResultInfo FnApprovalBack(ReqApprovalBack _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            if (!userLogic.CheckApproveSensPermission_I(_reqInfo.memberid, _reqInfo.comcd))
            {
                resp.statusCode = 90023;
                resp.statusMsg = "无操作权限";
                return resp;
            }

            resp = financialLogic.FnApproveBack(_reqInfo.memberid, _reqInfo.applyid, _reqInfo.reason);
            return resp;
        }

        /// <summary>
        /// 业务账单状态退回，用于审批完结后的账单特殊处理
        /// 敏感操作，一退到底，不受理非业务类账单
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/financial/approvalbackbill")]
        public RespFinaNullResultInfo BillApprovalBack(ReqApprovalBack _reqInfo)
        {
            RespFinaNullResultInfo resp = new RespFinaNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            if (!userLogic.CheckApproveSensPermission_I(_reqInfo.memberid, _reqInfo.comcd))
            {
                resp.statusCode = 90023;
                resp.statusMsg = "无操作权限";
                return resp;
            }

            resp = financialLogic.BillApproveBack(_reqInfo.memberid, _reqInfo.applyid, _reqInfo.reason);
            return resp;
        }

    }
}
